﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcLibrary.Repository;
using System.IO;
using WebVideo.Areas.Admin.Models;

namespace WebVideo.Areas.Admin.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /Admin/News/

        public ActionResult Index()
        {
            
            return View(NewsModel.LayDuLieu ());
        }
         #region "Insert"
        public ActionResult Insert()
        {
             NewsModel tnew = new NewsModel();
            return View(tnew);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(NewsModel Mtnew,HttpPostedFileBase Image)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Hình ảnh phải bé hơn 2MB." });
                        }
                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Không thể Upload hình" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images/TinTuc"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }

                    NewsModel.Insert(Mtnew);

                    return RedirectToAction("Index", "News");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //insert


        #region "Edit"
        public ActionResult Edit(int IdNews)
        {
            return View(NewsModel.LayModel (IdNews));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(NewsModel Mtnew, HttpPostedFileBase Image)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Hình ảnh phải bé hơn 2MB." });
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Không thể Upload hình" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images/TinTuc"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }
                  
                   string HinhAnh= NewsModel.Edit (Mtnew);
                    if (HinhAnh != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/TinTuc"), HinhAnh); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    return RedirectToAction("Index", "News");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //Edit

        #region "Delete"

        public string Delete(int Id)
        {
            try
            {
               string image =  NewsModel.Delete(Id);
               if (image != null)
               {
                   string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/TinTuc"), image); // file hinh cu
                   System.IO.File.Delete(fileToDelete);
               }
              
                return "ok";

            }
            catch
            {
                return "error";

            }

        }

        #endregion //Delete
    }
    
}
